package com.aynu.stack;

/**
 * @author mj
 * @version 1.0
 * @date 2020/12/13 23:21
 */
public class ArrayStackTest {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.put(1);
        arrayStack.put(2);
        arrayStack.put(3);
        arrayStack.put(4);
        arrayStack.put(5);
        arrayStack.put(6);

        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();
        arrayStack.pop();

        arrayStack.list();
    }
}

class ArrayStack {
    public int maxSize;   //数组的大小
    public int[] stack;   //定义一个栈
    public int top = -1;  //表示栈顶，-1表示没有数据

    //构造方法
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //栈满判断
    public boolean isMan() {
        if (this.top == maxSize - 1) {
            return true;
        }
        return false;
    }

    //栈空判断
    public boolean isEmpty() {
        if (top == -1) {
            return true;
        }
        return false;
    }

    //入栈
    public void put(int num){
        if (isMan()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = num;
    }

    //出栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历栈
    public void list(){
        if (isEmpty()){
            System.out.println("栈为空");
            return;
        }
        for (int i : stack) {
            System.out.println(i);
        }
    }
}
